﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZLDC.Common.Estate;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ZLDC.Topo.BLL.Estate
{
    public class lp_YesterDay
    {
        public static void GetBtnList(TextBox txtTime, Button btnUP)
        {
                                                                                            }

                                 public static void Getjcts(System.Web.UI.HtmlControls.HtmlGenericControl a1,TextBox txtTime,Button btnUP,string opentype,string deptid)
        {
            string sqlwhere = " select CONVERT(varchar,workDay,23) as workDay from lp_Configuration_main where deptid=" + deptid + " and isConfirm=0 and   DATEDIFF(dd, workDay, GETDATE()) != 0 and  workDay<CONVERT(varchar,GETDATE(),23) group by CONVERT(varchar,workDay,23)";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sqlwhere);
            if (ds.Tables[0].Rows.Count > 0)
            {
                a1.InnerHtml = "您有<a onclick=\"dateat("+deptid+")\" style='font-weight:bold;color:red;font-size:20px;'>" + ds.Tables[0].Rows.Count + "</a>天未检查工作!";
               
                                                                            }
                
        }

                                 public static void GetNum(DateTime dt, System.Web.UI.HtmlControls.HtmlGenericControl Average, System.Web.UI.HtmlControls.HtmlGenericControl YesterDay, string childdept, System.Web.UI.HtmlControls.HtmlGenericControl District, string deptid, string depttype,string userid)
        {
            string sqlwhere = "select SUM(Actual_Goal) as Actual_Goal,COUNT(1) as count from lp_Configuration_main where deptID in (" + deptid + ")  and DATEDIFF(dd, workday, '" + dt.ToString("yyyy-MM-dd") + "') = 0";
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sqlwhere);
            decimal num = 0;
            if (ds1.Tables[0].Rows.Count > 0)
            {
                if (ds1.Tables[0].Rows[0]["Actual_Goal"].ToString() != "" && ds1.Tables[0].Rows[0]["count"].ToString() != "0")
                    num = Convert.ToDecimal(ds1.Tables[0].Rows[0]["Actual_Goal"].ToString()) / Convert.ToDecimal(ds1.Tables[0].Rows[0]["count"].ToString());            }
            sqlwhere = "select *from lp_Configuration_main where deptid="+deptid+" and CONVERT(varchar, createTime, 23 )>'" + dt.AddDays(-6).ToString("yyyy-MM-dd") + "' and CONVERT(varchar, createTime, 23 )!='" + dt.ToString("yyyy-MM-dd") + "' and createuser is not null";
            ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sqlwhere);
            Average.InnerHtml = GetZT(ds1, Average, num);           
            string deptIdList = childdept;
            ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds("select SUM(Actual_Goal) as Actual_Goal,deptID from lp_Configuration_main where deptID in (" + deptIdList + ") and DATEDIFF(dd, workday, '" + dt.ToString("yyyy-MM-dd") + "') = 0 group by deptID order by SUM(Actual_Goal) desc");
            District.InnerHtml = "排名：<a style=\"color: Red; font-size: 20px;\">";
            int ii = 0;
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                if (ds1.Tables[0].Rows[i]["Actual_Goal"].ToString() != "0")
                {
                    if (deptid == ds1.Tables[0].Rows[i]["deptID"].ToString())
                    {
                        ii = i + 1;
                        break;
                    }
                }

            }
            switch (depttype)
            {
                case "分行":
                    depttype = "组别";
                    break;
            }
            YesterDay.InnerHtml = "<div>" + depttype + "昨日平均得分：<a style=\"font-size: 20px;\">" + num.ToString("0.00") + "</a></div>";

            District.InnerHtml += ii + "</a>";
        }

                                public static void GetNum1(DateTime dt, System.Web.UI.HtmlControls.HtmlGenericControl Average, System.Web.UI.HtmlControls.HtmlGenericControl YesterDay, string childdept, System.Web.UI.HtmlControls.HtmlGenericControl District, string deptid, string depttype,string userid)
        {
            string sqlwhere = "select * from lp_Configuration_main where userid=" + userid + " and DATEDIFF(dd, workday, '" + dt.ToString("yyyy-MM-dd") + "') = 0";
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sqlwhere);
            int num = 0;            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                num = num + int.Parse(ds1.Tables[0].Rows[i]["Actual_Goal"].ToString());
            }
            if (ds1.Tables[0].Rows.Count > 0)
                num = num / ds1.Tables[0].Rows.Count;            sqlwhere = "select top 5 * from lp_Configuration_main where userid=" + userid + " and CONVERT(varchar, createTime, 23 )>'" + dt.AddDays(-6).ToString("yyyy-MM-dd") + "' and CONVERT(varchar, createTime, 23 )!='" + dt.ToString("yyyy-MM-dd") + "' and leave !=1 order by createTime desc";
            ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sqlwhere);
            Average.InnerHtml = GetZT(ds1, Average, num);            

            string deptIdList = childdept;
            ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds("select Actual_Goal as Actual_Goal,userID  from lp_Configuration_main where " + GetDeptFile(depttype, deptid) + "   and DATEDIFF(dd, workday, '" + dt.ToString("yyyy-MM-dd") + "') = 0  order by Actual_Goal desc");
            District.InnerHtml = "排名：<a style=\"color: Red; font-size: 20px;\">";
            int ii = 0;
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                if (ds1.Tables[0].Rows[i]["Actual_Goal"].ToString() != "0")
                {
                    if (userid == ds1.Tables[0].Rows[i]["userID"].ToString())
                    {
                        ii = i + 1;
                        break;
                    }
                }
            }
            switch (depttype)
            {
                case "分行":
                    depttype = "个人";
                    break;
            }
            YesterDay.InnerHtml = "<div>" + depttype + "昨日得分：<a style=\"font-size: 20px;\">" + num.ToString("0.00") + "</a></div>";
            District.InnerHtml += ii + "</a>";
        }


                                                        public static string GetDeptFile(string depttype,string deptid)
        {
            string filewhere = "";
            switch (depttype)            {
                case "分行":
                    filewhere = "deptid in (" + deptid + ")  ";
                    break;
                case "组别":
                    filewhere = "deptid in (" + deptid + ")  ";
                    break;
                case "区域":
                    filewhere = "Other4 in (" + deptid + ")  ";
                    break;
                case "营业部":
                    filewhere = "Other6  in (" + deptid + ")  ";
                    break;

            }
            return filewhere;
        }

                                                        public static string GetDeptAverage(string depttype, string deptid)
        {
            string filewhere = "";
            switch (depttype)            {
                case "分行":
                    filewhere = "deptid in (" + deptid + ")  ";
                    break;
                case "组别":
                    filewhere = "deptid in (" + deptid + ")  ";
                    break;
                case "区域":
                    filewhere = "deptid in (" + deptid + ")  ";
                    break;
                case "营业部":
                    filewhere = "Other4 in (" + deptid + ")  ";
                    break;

            }
            return filewhere;
        }

                                public static void GetNum(DateTime dt, System.Web.UI.HtmlControls.HtmlGenericControl Average, System.Web.UI.HtmlControls.HtmlGenericControl YesterDay, System.Web.UI.HtmlControls.HtmlGenericControl District, string deptid, string depttype,string deptid_1,string pmtjDeptid)
        {

            string sqlwhere = "select SUM(Actual_Goal) as Actual_Goal,COUNT(1) as count from lp_Configuration_main where " + GetDeptAverage(depttype,deptid) + "  and DATEDIFF(dd, workday, '" + dt.ToString("yyyy-MM-dd") + "') = 0";
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sqlwhere);
            
            decimal num = 0;
            if (ds1.Tables[0].Rows.Count > 0)
            {
                if (ds1.Tables[0].Rows[0]["Actual_Goal"].ToString() != "" && ds1.Tables[0].Rows[0]["count"].ToString() != "0")
                    num = Convert.ToDecimal(ds1.Tables[0].Rows[0]["Actual_Goal"].ToString()) / Convert.ToDecimal(ds1.Tables[0].Rows[0]["count"].ToString());            }

            string filewhere = "";
            switch (depttype)            {
                case "分行":
                    depttype = "组别";
                    filewhere = "deptid";
                    break;
                case "区域":
                    filewhere = "Other4 ";
                    break;
                case "营业部":
                    filewhere = "Other6";
                    break;

            }
           
            sqlwhere = "select *from lp_Configuration_main where "+filewhere+" in ("+deptid+") and  CONVERT(varchar, createTime, 23 )>'" + dt.AddDays(-6).ToString("yyyy-MM-dd") + "' and CONVERT(varchar, createTime, 23 )!='" + dt.ToString("yyyy-MM-dd")+"'";
            ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sqlwhere);
            Average.InnerHtml = GetZT(ds1, Average, num);
            YesterDay.InnerHtml = "<div>"+depttype+"昨日平均得分：<a style=\"font-size: 20px;\">" + num.ToString("0.00") + "</a></div>";

            if (depttype != "营业部")            {
                ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds("select SUM(Actual_Goal) as Actual_Goal," + filewhere + " from lp_Configuration_main where " + GetDeptFile(depttype, pmtjDeptid) + "  and DATEDIFF(dd, workday, '" + dt.ToString("yyyy-MM-dd") + "') = 0 group by " + filewhere + " order by SUM(Actual_Goal) desc");
                District.InnerHtml = "排名：<a style=\"color: Red; font-size: 20px;\">";
                int ii = 0;
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    if (ds1.Tables[0].Rows[i]["Actual_Goal"].ToString() != "0")
                    {
                        if (deptid_1== ds1.Tables[0].Rows[i][1].ToString())
                        {
                            ii = i + 1;
                            break;
                        }
                    }

                }
                District.InnerHtml += ii + "</a>";
            }
        }


                                                                public static string GetZT(DataSet ds, System.Web.UI.HtmlControls.HtmlGenericControl Average,decimal num)
        {
            int fnum = 0;            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    fnum = fnum + int.Parse(ds.Tables[0].Rows[i]["Actual_Goal"].ToString());
                }
            }
            fnum = fnum / 5;
            Average.InnerHtml = "<div >近五日得分有所:";
            if (fnum < num)
            {
                Average.InnerHtml += "上升";
            }
            if (fnum > num)
            {
                Average.InnerHtml += "下降";
            }
            if (fnum == num)
            {
                Average.InnerHtml += "持平";
            }
            Average.InnerHtml += "</div>";
            return Average.InnerHtml;
        }
                                 public static void GetList(DateTime dt1, Repeater RpList, Button btnUP, string deptid,string type)
        {
            
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds("select * from lp_Configuration_main where deptID=" +deptid + " and DATEDIFF(dd, workday, '" + dt1.ToString("yyyy-MM-dd") + "') = 0 order by Actual_Goal desc,leave,choiceType asc ");
           
            RpList.DataSource = ds1;
            RpList.DataBind();
            int rowisconfirm = ds1.Tables[0].Rows.Count;            int rowstrue = 0;
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                if (ds1.Tables[0].Rows[i]["userID"].ToString() == "1")
                { rowstrue++; }
                foreach (RepeaterItem Item in RpList.Items)
                {
                    HiddenField hidUserid = (HiddenField)Item.FindControl("hidUserid");
                    TextBox txttotal = (TextBox)Item.FindControl("txttotal");
                    TextBox Actual_Goal = (TextBox)Item.FindControl("txtActual_Goal");
                    TextBox txtRemark = (TextBox)Item.FindControl("txtRemark");
                    CheckBox ckb = (CheckBox)Item.FindControl("ckb");
                    HiddenField hidOther1 = (HiddenField)Item.FindControl("hidOther1");
                    if (ds1.Tables[0].Rows[i]["userID"].ToString() == hidUserid.Value)
                    {
                        hidOther1.Value = ds1.Tables[0].Rows[i]["Other1"].ToString();
                        txttotal.Text = ds1.Tables[0].Rows[i]["total"].ToString();
                        Actual_Goal.Text = ds1.Tables[0].Rows[i]["Actual_Goal"].ToString();
                        if (int.Parse(Actual_Goal.Text) > 0)
                        {
                            Actual_Goal.Style["Color"] = "#FF0066";
                        }
                        
                        if (ds1.Tables[0].Rows[i]["isComplete"].ToString() == "1")
                        {
                            ckb.Checked = true;
                        }
                    }
                }
            }
            if (type == "")
            {
               
                    if (rowisconfirm==rowstrue)
                    {
                        btnUP.Enabled = false;
                    }
                    else
                    {
                        btnUP.Enabled = true;
                    }
             
            }
        }

        public static void BtnUp(Repeater RpList,Page p,TextBox txtTime)
        {
            string sql = "";
            foreach (RepeaterItem Item in RpList.Items)
            {
                HiddenField hidUserid = (HiddenField)Item.FindControl("hidUserid");
                TextBox txttotal = (TextBox)Item.FindControl("txttotal");
                TextBox Actual_Goal = (TextBox)Item.FindControl("txtActual_Goal");
                                CheckBox ckb = (CheckBox)Item.FindControl("ckb");
                sql += " update lp_Configuration_main set ";
                                sql += " isConfirm=1";
                sql += " where userID=" + hidUserid.Value + " and DATEDIFF(dd, workday, '" + Convert.ToDateTime(txtTime.Text).ToString("yyyy-MM-dd") + "') = 0;";
            }
            int row = ZLDC.Topo.BLL.Estate.lp_Basis.Execution_sql(sql);
                                        p.Response.Write("<script>alert('昨日工作检查完毕！');window.location.href=location.href;</script>");
                    }


                                                  public static void GetCharts(Literal mynext, TextBox txtTime, string deptid, HiddenField hidchart)
        {
 

            StringBuilder sb1 = new StringBuilder();
            string sql = "select BasisName,sum(Actual_Goal) as Actual_Goal,sum(Point )as Point,sum(Actual_Number) as Actual_Number   from lp_Configuration where Other2=" + deptid + " and CONVERT(varchar,CreateTime,23) ='" + txtTime.Text + "' and (Actual_Goal>0 or Actual_Number>0)  group by  BasisName";
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ds1.Tables.Count > 0)
            {
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    sb1.Append("{name:'" + ds1.Tables[0].Rows[i]["BasisName"].ToString() + ds1.Tables[0].Rows[i]["Actual_Number"].ToString() + "',");
                    sb1.Append(" y:" + ds1.Tables[0].Rows[i]["Actual_Goal"].ToString() + ", ");
                    sb1.Append("dataLabels:'" + ZLDC.Topo.BLL.Estate.lp_Basis.Get_ChartUrl1(ds1.Tables[0].Rows[i]["BasisName"].ToString()) + "&deptid=" + deptid + "&addDate2=" + txtTime.Text + "&addDate1=" + txtTime.Text + "'},");

                }
            }
            hidchart.Value = sb1.ToString();
        }

                                                   public static string YJ(string userid,TextBox txtTime)
        {
            string sql = "select top 3 workDay into #temp_workDay from lp_Configuration_main where leave=0  and userID="+userid+" ";
            sql += " and workDay<'"+Convert.ToDateTime(txtTime.Text)+"'  order by workDay desc ";
            sql += " select * from lp_Configuration_main where workDay in ( ";
            sql += " select workDay from #temp_workDay )";
            sql += " and Actual_Goal < total  and userID='32014' ";
            sql += " drop table #temp_workDay ";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
            {
                if (ds.Tables[0].Rows.Count==3)
                return ""+ds.Tables[0].Rows.Count+"日未完成要求分数！";
            }
            return "";
        }

       
    }
}
